package io.wilson.rabbit.producer;

import cn.hutool.json.JSONObject;
import io.springframework.common.response.ServerResponse;
import io.wilson.rabbit.common.constant.RabbitMQConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;

@Slf4j
@RestController
@RequestMapping("/send")
public class SimpleSendController {
    @Resource
    private AmqpTemplate amqpTemplate;

    @PostMapping("/direct")
    public void sendDirect(@RequestParam String msg) {
        amqpTemplate.convertAndSend(RabbitMQConstant.DIRECT_EXCHANGE_NAME, RabbitMQConstant.DIRECT_ROUTING_NAME, msg);
    }

    @PostMapping("/fanout")
    public void sendFanout(@RequestParam String msg) {
        amqpTemplate.convertAndSend(RabbitMQConstant.VOC_EXCHANGE_NAME, StringUtils.EMPTY, msg);
    }

    @PostMapping("/topic/one")
    public void sendTopic(@RequestParam String msg) {
        amqpTemplate.convertAndSend(RabbitMQConstant.TOPIC_EXCHANGE_NAME, RabbitMQConstant.TOPIC_ROUTING_ONE_PATTERN, msg);
    }

    @PostMapping("/topic/more")
    public void sendTopicMore(@RequestParam String msg) {
        amqpTemplate.convertAndSend(RabbitMQConstant.TOPIC_EXCHANGE_NAME, RabbitMQConstant.TOPIC_ROUTING_MORE_PATTERN, msg);
    }

    @PostMapping("/topicA")
    public void sendTopicA(@RequestParam String msg) {
        amqpTemplate.convertAndSend(RabbitMQConstant.TOPIC_EXCHANGE_NAME, RabbitMQConstant.TOPIC_ROUTING_KEY_A, msg);
    }

    @PostMapping("/topicB")
    public void sendTopicB(@RequestParam String msg) {
        amqpTemplate.convertAndSend(RabbitMQConstant.TOPIC_EXCHANGE_NAME, RabbitMQConstant.TOPIC_ROUTING_KEY_B, msg);
    }

    @PostMapping("/topicC")
    public void sendTopicC(@RequestParam String msg) {
        amqpTemplate.convertAndSend(RabbitMQConstant.TOPIC_EXCHANGE_NAME, RabbitMQConstant.TOPIC_ROUTING_KEY_C, msg);
    }

    @PostMapping("/direct-i")
    public ServerResponse<?> sendSequenceDirect(@RequestParam String routingKey, @RequestParam Integer msg) throws JSONException {
        JSONObject jsonObject = new JSONObject();
        for (int i = 0; i < msg; i++) {
            jsonObject.set("msg", i);
            amqpTemplate.convertAndSend(RabbitMQConstant.SEQUENCE_DIRECT_EXCHANGE, routingKey, MessageBuilder
                    .withBody(jsonObject.toString().getBytes(StandardCharsets.UTF_8))
                    .build());
            log.info("send: {}", jsonObject);
        }
//        amqpTemplate.convertAndSend(RabbitMQConstant.SEQUENCE_DIRECT_EXCHANGE, routingKey, jsonObject.toString());
        return ServerResponse.of(200, "ok");
    }
}
